<?php
define('DEBUG', true);
// Turn register globals off
function al_unregister_GLOBALS() {
    if (!ini_get('register_globals')) {
        return;
    }
    if (isset($_REQUEST['GLOBALS'])) {
        die('GLOBALS overwrite attempt detected');
    }
    // Variables that shouldn't be unset
    $noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');
    $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
    foreach ($input as $k => $v) {
        if (!in_array($k, $noUnset) && isset($GLOBALS[$k])) {
            $GLOBALS[$k] = NULL;
            unset($GLOBALS[$k]);
        }
    }
}

al_unregister_GLOBALS();
if (DEBUG) {
    error_reporting(E_ALL);
} else {
    error_reporting(0);
}

define('LIBS', ARLICLE . 'libs/');
define('TMP_PATH', ROOT . 'www/tmp/');
define('ARLICLE_ELEMENT', ARLICLE . 'app/view/elements/');
define('ARLICLE_COMPONENT', ARLICLE . 'app/controller/components/');
define('ARLICLE_WWW', ROOT . 'www/');

require LIBS . 'plugin.php';
if (file_exists(ROOT . 'www/config/cache.php')) {
    require ROOT . 'www/config/cache.php';
}

if (defined('CACHE')) {
    if (CACHE) {
        require LIBS . 'cache.php';
    }
} else {
    define('CACHE', true);
}

if (!defined('CACHE_PATH')) {
    define('CACHE_PATH', TMP_PATH . 'cache/');
}

if (!defined('CACHE_TIMEOUT')) {
    define('CACHE_TIMEOUT', 60);
}

require LIBS . 'functions.php';
if (!file_exists(ROOT . 'www/config/database.php')) {
    $base = str_replace('index.php', '', env('SCRIPT_NAME'));
    die(__("It seemed that you haven't installed Arlicle. You can <a href='{$base}www/install/'>install it through a web interface</a>."));
}

if (file_exists(ROOT . 'www/config/admin.php')) {
    require ROOT . 'www/config/admin.php';
}

require ROOT . 'www/config/database.php';

if (!defined('DEFAULT_LANGUAGE')) {
    define('DEFAULT_LANGUAGE', 'en');
}

require LIBS . 'model/aldb.php';

$aldb->prefix = TABLE_PREFIX;
$aldb->capabilities = $aldb->prefix . 'capabilities';
$aldb->comments = $aldb->prefix . 'comments';
$aldb->users = $aldb->prefix . 'users';
$aldb->posts = $aldb->prefix . 'posts';
$aldb->groups = $aldb->prefix . 'groups';
$aldb->options = $aldb->prefix . 'options';
$aldb->usermeta = $aldb->prefix . 'usermeta';
$aldb->customfields = $aldb->prefix . 'customfields';
$aldb->customvalues = $aldb->prefix . 'customvalues';
$aldb->diagram = $aldb->prefix . 'diagram';
$aldb->post2category = $aldb->prefix . 'post2category';
$aldb->sessions = $aldb->prefix . 'sessions';
$aldb->attachments = $aldb->prefix . 'attachments';
$GLOBALS['aldb'] = $aldb;

require LIBS . 'formatting.php';
require LIBS . 'object.php';
require ARLICLE . 'dispatcher.php';
require LIBS . 'router.php';
require LIBS . 'acl.php';
require LIBS . 'kses.php';

$language_file = ROOT . 'www/languages/' . get_option('default_language') . '.php';
if (file_exists($language_file)) {
    require $language_file;
}

$al_active_plugins = get_option('active_plugins');
if (!empty($al_active_plugins)) {
    foreach($al_active_plugins as $active_plugin) {
        if (file_exists(ROOT . 'www/plugins/' . $active_plugin . '/ini.php')) {
            require ROOT . 'www/plugins/' . $active_plugin . '/ini.php';
            if (file_exists(ROOT . 'www/plugins/' . $active_plugin . '/libs/functions.php')) {
                require ROOT . 'www/plugins/' . $active_plugin . '/libs/functions.php';
            }
        }
    }
}

require LIBS . 'plugable.php';
define('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_option('site_url') . '/' ));
define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_option('site_url') . '/'));
define('COOKIE_DOMAIN', false);

$cookiehash = md5(get_option('site_url'));
define('COOKIEHASH', $cookiehash);
define('USER_COOKIE', 'arlicle_user_' . COOKIEHASH);
define('PASS_COOKIE','arlicle_pass_' . COOKIEHASH);

if (!get_magic_quotes_gpc()) {
    $_POST = add_magic_quotes($_POST);
    $_GET = add_magic_quotes($_GET);
    $_COOKIE = add_magic_quotes($_COOKIE);
    $_SERVER = add_magic_quotes($_SERVER);
}

$Dispatcher = new Dispatcher();
$Dispatcher->dispatch();
?>
